home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1997 #3 / Amiga Plus CD - 1997 - No. 03.iso / pd / demo-versionen / maxoncpp4-demo / demo / supercode / score.c < prev    next >
C/C++ Source or Header  |  1996-12-31  |  7KB  |  367 lines

  1. //-------------------------------------
  2. //
  3. // SuperCode (c) 1996 by T.Kühn 
  4. //
  5. // Programmiersprache:    ANSI-C
  6. // Projektstart:            30.07.95
  7. //
  8. // Modul:                Score
  9. //
  10. //-------------------------------------
  11.  
  12.  
  13.  
  14. #include <pragma/intuition_lib.h>
  15. #include <pragma/graphics_lib.h>
  16. #include <pragma/exec_lib.h>
  17. #include <pragma/dos_lib.h>
  18. #include <struct.h>
  19.  
  20.  
  21. //-------------------------------------
  22.  
  23. struct tkList highscores;
  24.  
  25. UBYTE str_day[LEN_DATSTRING+2];
  26. UBYTE str_date[LEN_DATSTRING+2];
  27. UBYTE str_time[LEN_DATSTRING+2];
  28.  
  29. struct DateTime date;
  30.  
  31. UBYTE file_name[]="PROGDIR:SuperCode.score";
  32.  
  33. struct Window *window_score=0;
  34.  
  35. struct ScoreData
  36. {
  37.     struct RastPort    *rp;
  38.     ULONG                    x,y,w,y2;
  39.     ULONG                 count;
  40. };
  41.  
  42. struct Score *score_last=0;
  43. void score_windowopen();
  44.  
  45. //-------------------------------------
  46. void score_savenode(struct Score *score_org,struct tkfile *file)
  47. {
  48.     const ULONG l=sizeof(struct Score);
  49.     ULONG t;
  50.     BYTE score[l+2];
  51.  
  52.     memcpy(score,score_org,l);
  53.  
  54.     file_write(file,"NEXT",4);
  55.  
  56.     for (t=0;t<l;t++)
  57.     {
  58.         score[t]^=t+0xEB;
  59.     }
  60.  
  61.     file_write(file,score,l);
  62.  
  63. }
  64. //-------------------------------------
  65. void score_save()
  66. {
  67.     struct tkfile *file;
  68.  
  69.     file = file_open(file_name,MODE_NEWFILE,FALSE);
  70.     if (file)
  71.     {
  72.         List_DoAll(&highscores,(APTR)score_savenode,(APTR)file);
  73.     }
  74.     file_free(&file);
  75.  
  76. }
  77. //-------------------------------------
  78. void score_load()
  79. {
  80.     struct tkfile *file;
  81.  
  82.     file = file_open(file_name,MODE_OLDFILE,ASKFILE_CANTOPEN);
  83.     {
  84.         const ULONG l=sizeof(struct Score);
  85.         ULONG t;
  86.         BYTE dummy[6],*new;
  87.     
  88.         while(file->ok)
  89.         {
  90.             dummy[0]=0;
  91.             file_read(file,dummy,4);
  92.             if (0!=strncmp(dummy,"NEXT",4)) break;
  93.  
  94.             new=Memory_Alloc(l);
  95.             file_read(file,new,l);
  96.  
  97.             for (t=0;t<l;t++)
  98.             {
  99.                 new[t]^=t+0xEB;
  100.             }
  101.             List_AddTail(&highscores,(APTR)new);
  102.         }
  103.     }
  104.     file_free(&file);
  105. }
  106. //-------------------------------------
  107.  
  108.  
  109. //-------------------------------------
  110. long score_compare(struct Score *score1,struct Score *score2)
  111. {
  112.     LONG back=0;
  113.  
  114.     if (score1->score > score2->score) back=-1;
  115.     if (score1->score < score2->score) back= 1;
  116.  
  117.     if (back==0)
  118.     {
  119.         if (score1->time < score2->time) back=-1;
  120.         if (score1->time > score2->time) back= 1;
  121.     }
  122.  
  123.     return back;
  124. }
  125. //-------------------------------------
  126.  
  127.  
  128. //-------------------------------------
  129. void score_output(struct Score *score,struct ScoreData *data)
  130. {
  131.     UBYTE text[200],*text_order;
  132.     ULONG c;
  133.  
  134.     ULONG t=score->time;
  135.     ULONG h=(t/3600);
  136.     ULONG m=(t-h*3600)/60;
  137.     ULONG s=(t-m*60+h*3600);
  138.  
  139.     if (data->y < data->y2)
  140.     {
  141.         if (score_last!=score)
  142.         {
  143.             c=Scrn.DrawInfo->dri_Pens[TEXTPEN];
  144.         }
  145.         else
  146.         {
  147.             c=Scrn.DrawInfo->dri_Pens[SHINEPEN];
  148.         }
  149.  
  150.         switch (score->order)
  151.         {
  152.             case ORDER_POS:         text_order=CatStr(TXT_PREFPOSITION);break;
  153.             case ORDER_DESC:        text_order=CatStr(TXT_PREFDESC);break;
  154.             case ORDER_RANDOM:    text_order=CatStr(TXT_PREFRANDOM);break;
  155.         }
  156.  
  157.         memcpy(&date.dat_Stamp,&score->date,sizeof(struct DateStamp));
  158.         date.dat_Format=FORMAT_CDN;
  159.         date.dat_Flags=DTF_SUBST;
  160.         date.dat_StrDay  = str_day;
  161.         date.dat_StrDate = str_date;
  162.         date.dat_StrTime = str_time;
  163.         DateToStr(&date);
  164.  
  165.         sprintf(text,"%2.2d %-10.10s %9.9d %2.2d:%02.2d:%02.2d %8.8s %2.2d %2.2d %2.2d %2.2d %6.6s",data->count++,score->name,score->score,h,m,s,str_date,score->lines,score->colors,score->columns,score->equals,text_order);
  166.     
  167.         Txt_PrintFit(data->rp,text,0,data->x,data->y,data->w,0,c,Scrn.DrawInfo->dri_Pens[BACKGROUNDPEN],BOX_LEFT);
  168.     
  169.         data->y+=data->rp->TxHeight+2;
  170.     }
  171. }
  172. //-------------------------------------
  173.  
  174.  
  175. //-------------------------------------
  176. void score_display()
  177. {
  178.     score_windowopen();
  179.     {
  180.         struct Window *win=window_score;
  181.         struct RastPort *rp=window_score->RPort;
  182.         ULONG x=win->BorderLeft+4;
  183.         ULONG y=win->BorderTop+4;
  184.         ULONG w=win->Width-win->BorderLeft-win->BorderRight-8;
  185.         ULONG y2=win->Height-win->BorderBottom-rp->TxHeight-8;
  186.         struct ScoreData data={rp,x,y,w,y2,1};
  187.  
  188.         SetAPen(rp,Scrn.DrawInfo->dri_Pens[BACKGROUNDPEN]);
  189.         RectFill(rp,x,y,x+w,y2);
  190.  
  191.         Txt_PrintFit(rp,CatStr(TXT_SCORETITLE),0,x,y,w,0,Scrn.DrawInfo->dri_Pens[HIGHLIGHTTEXTPEN],Scrn.DrawInfo->dri_Pens[BACKGROUNDPEN],BOX_CENTER);
  192.  
  193.         data.y+=rp->TxHeight+4;
  194.         data.count=1;
  195.  
  196.         List_DoAll(&highscores,(APTR)score_output,(APTR)&data);
  197.     }
  198.  
  199. }
  200. //-------------------------------------
  201.  
  202.  
  203. //-------------------------------------
  204. void score_windowopen()
  205. {
  206.     if (!window_score)
  207.     {
  208.         window_score=OpenWindowTags(0,
  209.             WA_Title,CatStr(TXT_SCOREWINTITLE),
  210.     
  211.             WA_Left,prg_prefs->win.score.Xpos,
  212.             WA_Top,prg_prefs->win.score.Ypos,
  213.             WA_Width,prg_prefs->win.score.WinW,
  214.             WA_Height,prg_prefs->win.score.WinH,
  215.             (Scrn.Scrn && Scrn.FlgPublic==FALSE) ? WA_CustomScreen : WA_PubScreen,Scrn.Scrn,
  216.             WA_Flags,
  217.                     WFLG_ACTIVATE|
  218.                     WFLG_CLOSEGADGET|
  219.                     WFLG_DRAGBAR|
  220.                     WFLG_DEPTHGADGET|
  221.                     WFLG_NEWLOOKMENUS|
  222.                     WFLG_RMBTRAP|
  223.     //                WFLG_SIZEBBOTTOM|
  224.                     WFLG_SIZEBRIGHT|
  225.                     WFLG_SIZEGADGET|
  226.                     0,
  227.             WA_IDCMP,
  228.                     IDCMP_GADGETUP|
  229.                     IDCMP_CLOSEWINDOW|
  230.                     IDCMP_NEWSIZE|
  231.                     0,
  232. //            WA_Zoom,0,
  233.             WA_AutoAdjust,TRUE,
  234.             WA_MaxWidth,-1,
  235.             WA_MaxHeight,-1,
  236.             WA_MinWidth,50,
  237.             WA_MinHeight,50,
  238.             TAG_END);
  239.     }
  240. }
  241. //-------------------------------------
  242. void score_windowclose()
  243. {
  244.     if (window_score)
  245.     {
  246.         window_close(window_score,&prg_prefs->win.score);
  247.         window_score=0;
  248.     }
  249. }
  250. //-------------------------------------
  251.  
  252.  
  253. //-------------------------------------
  254. LONG score_getbit()
  255. {
  256.     LONG bit=0;
  257.  
  258.     if (window_score)
  259.     {
  260.         bit=1 << window_score->UserPort->mp_SigBit;
  261.     }
  262.  
  263.     return bit;
  264. }
  265. //-------------------------------------
  266. VOID score_domsg()
  267. {
  268.     struct Window *win=window_score;
  269.     struct IntuiMessage *msg;
  270.  
  271.     struct Gadget *gadg;
  272.     LONG clas;
  273.     LONG code;
  274.     LONG qual;
  275.     LONG posx,posy,seco,micr;
  276.  
  277.     while (window_score && (msg=(struct IntuiMessage*)GetMsg(win->UserPort)))
  278.     {
  279.         gadg = (APTR)msg->IAddress;
  280.         clas = msg->Class;
  281.         code = msg->Code;
  282.         qual = msg->Qualifier;
  283.         posx = msg->MouseX;
  284.         posy = msg->MouseY;
  285.         seco = msg->Seconds;
  286.         micr = msg->Micros;
  287.  
  288.         ReplyMsg((struct Message*)msg);
  289.  
  290.         switch (clas)
  291.         {
  292.             case IDCMP_CHANGEWINDOW:
  293. //                Window_StoreSize(MsgBlk);
  294.                 break;
  295.             case IDCMP_RAWKEY:
  296.                 break;
  297.             case IDCMP_NEWSIZE:
  298.                 score_display();
  299.                 break;
  300.             case IDCMP_MENUPICK:
  301. //                Command_Menu(MsgBlk,GadMsg.Code);
  302.                 break;
  303.             case IDCMP_CLOSEWINDOW:
  304.                 score_windowclose();
  305.                 break;
  306.             case IDCMP_GADGETUP:
  307. //                score_dogadget(gadg->GadgetID,qual);
  308.                 break;
  309.             case IDCMP_INACTIVEWINDOW:
  310.                 break;
  311.         }
  312.     }
  313. }
  314. //-------------------------------------
  315.  
  316.  
  317. //-------------------------------------
  318. void score_add(struct Field *player,UBYTE *name)
  319. {
  320.     struct Score *score;
  321.  
  322.     if (player->finish)
  323.     {
  324.         score=(struct Score*)Memory_Alloc(sizeof(struct Score));
  325.  
  326.         score_last=score;
  327.  
  328.         score->score=code_getpoints(player);
  329.         score->columns=player->columns;
  330.         score->colors=player->num_colors;
  331.         score->equals=player->max_equal;
  332.         score->lines=player->lines;
  333.         score->time=player->sec_finish - player->sec_start;
  334.         score->order=player->val_order;
  335.  
  336.         strncpy(score->name,name,MAX_PLAYERNAME);
  337.  
  338.         DateStamp(&score->date);
  339.  
  340.         List_AddSort(&highscores,&score->node,(APTR)score_compare);
  341.  
  342. //        List_AddTail(&highscores,&score->node);
  343. //        List_Sort(&highscores,(APTR)score_compare);
  344.  
  345.         score_save();
  346.         score_display();
  347.     }
  348. }
  349. //-------------------------------------
  350.  
  351.  
  352. //-------------------------------------
  353. void score_init()
  354. {
  355.     List_Init(&highscores,NT_SCORE,0);
  356.     score_load();
  357. }
  358. //-------------------------------------
  359. void score_free()
  360. {
  361.     score_windowclose();
  362.  
  363.     List_Free(&highscores);
  364. }
  365. //-------------------------------------
  366.  
  367.